<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="./bootstrap/css/bootstrap.min.css" />
  </head>

  <body>
    <div id="app">
      <div class="m-3">
        <div class="row">
          <div class="col-9">
            <input type="text" v-model="keyword" class="form-control" />
          </div>
          <div class="col-3">
            <button class="btn btn-primary" @click="getUsersList">搜索</button>
          </div>
        </div>
      </div>

      <table class="table">
        <thead>
          <tr>
            <th>用户名</th>
            <th>邮箱</th>
            <th>手机</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="{username,email,phone} in usersList">
            <td>{{username}}</td>
            <td>{{email}}</td>
            <td>{{phone}}</td>
          </tr>
        </tbody>
      </table>
    </div>
    <script type="module">
      import {
        createApp,
        ref,
        reactive,
        computed,
        watch,
        watchEffect,
      } from "./vue.esm-browser.js";
      // 链接数据库
      import PocketBase from "./pocketbase.es.js";
      const pb = new PocketBase("http://127.0.0.1:8090");
      const app = createApp({
        setup() {
          const keyword = ref("");
          const usersList = ref([]); //保存用户列表
          async function getUsersList() {
            let filter = "";
            if (keyword !== "") {
              filter = `username~'${keyword.value}'`;
            }
            const resultList = await pb.collection("users").getList(1, 50, {
              filter,
            });
            usersList.value = resultList.items;
          }

          watch(
            keyword,
            () => {
              getUsersList();
            },
            { immediate: true }
          );

          return { keyword, usersList, getUsersList };
        },
      });

      app.mount("#app"); //挂载
    </script>
  </body>
</html>
